//
//  TTIPickerView.m
//  iOSCodeProject
//
//  Created by zym on 14-7-21.
//  Copyright (c) 2014年 翔傲信息科技（上海）有限公司. All rights reserved.
//

#import "TTIPickerView.h"
#import "AppMacro.h"
#import "UIColor+Utils.h"

#define VIEW_WIDTH [[UIScreen mainScreen] bounds].size.width
@interface TTIPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic, strong)UIPickerView *pickerView;
@property(nonatomic, strong)UIView *pickViewBG;

@end

@implementation TTIPickerView

-(id)init
{
    self=[super init];
    if (self) {
        
        
        self.frame=[[UIScreen mainScreen] bounds];
        
        self.pickViewBG=[[UIView alloc] init];
        _pickViewBG.frame=CGRectMake(0, self.frame.size.height, VIEW_WIDTH,260);
        [_pickViewBG setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:_pickViewBG];
        
        UIView *pickerToolbar = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, VIEW_WIDTH, 44.0)];
        [pickerToolbar setBackgroundColor:[UIColor  colorForHex:LIGHT_GRAY_COCOR]];
        [_pickViewBG addSubview:pickerToolbar];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setFrame:CGRectMake(0, 2, 60, 40)];
        [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
        [[leftBtn titleLabel] setFont:[UIFont boldSystemFontOfSize:15]];
        [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [leftBtn addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [pickerToolbar addSubview:leftBtn];
        
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(VIEW_WIDTH-60, 2, 60, 40)];
        [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
        [[rightBtn titleLabel] setFont:[UIFont boldSystemFontOfSize:15]];
        [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [pickerToolbar addSubview:rightBtn];
        
        
        //UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, VIEW_WIDTH, 44.0)];
       // pickerToolbar.barStyle=UIBarStyleBlackOpaque;
        //[_pickViewBG addSubview:pickerToolbar];
        
//        NSMutableArray *barItems = [[NSMutableArray alloc] init];
//        
//        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"  取消" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelButtonPressed:)];
//        [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
//        [barItems addObject:cancelBtn];
//        
//        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//        [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
//        [barItems addObject:flexSpace];
//        
//        UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithTitle:@"完成  " style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonPressed:)];
//        [barItems addObject:doneBtn];
//        
//        [pickerToolbar setItems:barItems animated:YES];
//        
        
        
        self.pickerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, VIEW_WIDTH, 216)];
        _pickerView.showsSelectionIndicator=YES;
        _pickerView.delegate=self;
        _pickerView.dataSource=self;
        [_pickViewBG addSubview:_pickerView];
        
    }
    return self;
}
-(void)setSourceArray:(NSMutableArray *)sourceArray
{
    _sourceArray=sourceArray;
    [_pickerView reloadAllComponents];
}

-(void)show:(void (^)(NSInteger, NSString *))block
{
    self.block = block;
    
    self.backgroundColor=[[UIColor grayColor] colorWithAlphaComponent:0.4];
    UIWindow *appWindow=[[UIApplication sharedApplication] keyWindow];
    [appWindow addSubview:self];
    
    [self animateIn];
}

-(void)animateIn
{
    [UIView animateWithDuration:0.4 animations:^{
        _pickViewBG.frame=CGRectMake(0, self.frame.size.height-260, VIEW_WIDTH,260);
    }];
}
-(void)animateOut
{
    [UIView animateWithDuration:0.4 animations:^{
        _pickViewBG.frame=CGRectMake(0, self.frame.size.height, VIEW_WIDTH,260);
        _pickViewBG.alpha=0.3;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)doneButtonPressed:(id)sender
{
    NSInteger row=[_pickerView selectedRowInComponent:0];
    NSString *title=[NSString stringWithFormat:@"%@",[_sourceArray objectAtIndex:row]];
    
    if (self.block) {
        self.block(row,title);
    }
    
    
    [self animateOut];
}
- (void)cancelButtonPressed:(id)sender
{
    [self animateOut];
}

#pragma  mark - UIPickerView Delegate

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_sourceArray count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%@",[_sourceArray objectAtIndex:row]];
}


@end
